package com.gr.staffpm.tasks.model;

import org.apache.wicket.model.LoadableDetachableModel;

import com.gr.staffpm.datatypes.TaskComment;
import com.gr.staffpm.tasks.service.TaskService;


public class DetachableCommentModel extends LoadableDetachableModel<TaskComment> {

    private final int id;
    private final TaskService taskService;
    private static final long serialVersionUID = 1L;

    public DetachableCommentModel(TaskService ts, TaskComment comment) {
        this.taskService = ts;
        this.id = comment.getCommentId();
    }

    @Override
    public int hashCode() {
        return Long.valueOf(id).hashCode();
    }

    /**
     * Used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     * 
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (obj instanceof DetachableCommentModel) {
            DetachableCommentModel other = (DetachableCommentModel) obj;
            return other.id == id;
        }
        return false;
    }


    @Override
    protected TaskComment load() {
        return taskService.getComment(id);
    }

}
